package com.pescamillam.kiosco.windows;

import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.pescamillam.kiosco.sql.Code;
import com.pescamillam.kiosco.sql.SqlConnection;

public class ExitFrame extends JFrame{
	
	/** auto uid */
	private static final long serialVersionUID = -1231762722451551040L;
	JLabel personNameLabel, exitMessageLabel, pictureLabel, barcodeLabel,
	serialLabel, deviceLabel, trademarckLabel,
	serialNameLabel, deviceNameLabel, trademarckNameLabel;
	JButton acceptButton;
	StringBuffer codeToAdd = new StringBuffer();
	private static ExitFrame instance;
	
	public static ExitFrame getInstance(String codeToSend) {
		if (instance == null) {
			instance = new ExitFrame(codeToSend);
			instance.setExtendedState(JFrame.MAXIMIZED_BOTH);
			instance.setUndecorated(true);
			instance.setVisible(true);
			instance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		instance.loadFrame(codeToSend);
		return instance;
	}
	
	public void loadFrame(String codeToSend) {
		Code codigo = SqlConnection.consultarCodigo(codeToSend);
		if (codigo != null) {
			personNameLabel.setText(codigo.nombreUsuario);
			Image image = null;
			try {
				image = ImageIO.read(codigo.imagen);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if (image != null) {
				pictureLabel.setIcon(new ImageIcon(image));
			}
			barcodeLabel.setText(codigo.codigo);
			deviceLabel.setText(codigo.dispositivo);
			trademarckLabel.setText(codigo.marca);
			serialLabel.setText(codigo.serial);
//			disableButton.setVisible(true);
			
			if (codigo.ticketEnable==null) {
//				disableButton.setVisible(false);
				JOptionPane.showMessageDialog(null, "CODIGO YA UTILIZADO!!!");
			}
			setVisible(true);
			Keyboard.getInstance().setVisible(false);
			SqlConnection.cambiarTicketEnable(barcodeLabel.getText());
		} else {
			JOptionPane.showMessageDialog(null, "Codigo " + codeToSend + " no encontrado");
			setVisible(false);
			Keyboard.getInstance().setVisible(true);
		}
	}
	
	private ExitFrame(String codeToSend) {
		setLayout(null);
		
		exitMessageLabel = new JLabel("Gracias por visitarnos");
		exitMessageLabel.setBounds(320, 20, 600, 50);
		exitMessageLabel.setFont(new Font("Arial", Font.PLAIN, 40));
		add(exitMessageLabel);
		
		personNameLabel =new JLabel("",JLabel.CENTER);
		personNameLabel.setBounds(50, 70, 950, 50);
		personNameLabel.setFont(new Font("Arial", Font.PLAIN, 35));
		add(personNameLabel);
		
		pictureLabel = new JLabel(new ImageIcon());
		pictureLabel.setBounds(294, 160, 640, 480);
		add(pictureLabel);
		
		barcodeLabel =new JLabel("",JLabel.CENTER);
		barcodeLabel.setBounds(50, 110, 950, 50);
		barcodeLabel.setFont(new Font("Arial", Font.PLAIN, 30));
		add(barcodeLabel);
		
		deviceNameLabel = new JLabel("Dispositivo:");
		deviceNameLabel.setBounds(40, 150, 200, 50);
		deviceNameLabel.setFont(new Font("Arial", Font.PLAIN, 25));
		add(deviceNameLabel);
		
		deviceLabel = new JLabel();
		deviceLabel.setBounds(40, 200, 200, 50);
		deviceLabel.setFont(new Font("Arial", Font.PLAIN, 25));
		add(deviceLabel);
		
		trademarckNameLabel = new JLabel("Marca:");
		trademarckNameLabel.setBounds(40, 300, 200, 50);
		trademarckNameLabel.setFont(new Font("Arial", Font.PLAIN, 25));
		add(trademarckNameLabel);
		
		trademarckLabel = new JLabel();
		trademarckLabel.setBounds(40, 350, 200, 50);
		trademarckLabel.setFont(new Font("Arial", Font.PLAIN, 25));
		add(trademarckLabel);
		
		serialNameLabel = new JLabel("Serial:");
		serialNameLabel.setBounds(40, 450, 200, 50);
		serialNameLabel.setFont(new Font("Arial", Font.PLAIN, 25));
		add(serialNameLabel);

		serialLabel = new JLabel();
		serialLabel.setBounds(10, 500, 280, 50);
		serialLabel.setFont(new Font("Arial", Font.PLAIN, 25));
		add(serialLabel);
		
//		if (serialLabel.getText()==""){
//			serialLabel.setVisible(false);
//			serialNameLabel.setVisible(false);
//			disableButton.setVisible(false);
//		}else{
//			serialLabel.setVisible(true);
//			serialNameLabel.setVisible(true);
//			disableButton.setVisible(true);
//		};

//		disableButton = new JButton();
//		disableButton.setText("Eliminar");
//		disableButton.setBounds(610, 680, 200, 80);
//		disableButton.setFont(new Font("Arial", Font.PLAIN, 30));
//		disableButton.setFocusable(false);
//		disableButton.setFocusTraversalKeysEnabled(false);
//		add(disableButton);
//		
//		disableButton.addActionListener(new ActionListener() {
//			
//			public void actionPerformed(ActionEvent arg0) {
				
//			}
//		});
		
		acceptButton = new JButton();
		acceptButton.setText("Aceptar");
		acceptButton.setBounds(310, 680, 200, 80);
		acceptButton.setFont(new Font("Arial", Font.PLAIN, 30));
		acceptButton.setFocusable(false);
		acceptButton.setFocusTraversalKeysEnabled(false);
		add(acceptButton);
		
		acceptButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Keyboard frame = Keyboard.getInstance();
		    	frame.setVisible(true);
		    	frame.resetNumbers();
		    	ExitFrame.this.setVisible(false);
			}
		});
		acceptButton.addKeyListener(new ScannerListener());
		acceptButton.setFocusable(true);
		addKeyListener(new ScannerListener());
	}
	
	
	
	public void addToCode(char charToAdd) {
		if (Character.isDigit(charToAdd)) {
			codeToAdd.append(charToAdd);
		}
	}
	
	public void reloadFrame() {
		loadFrame(codeToAdd.toString());
		codeToAdd = new StringBuffer();
	}
	
	private class ScannerListener implements KeyListener {
		@Override
		public void keyTyped(KeyEvent arg0) {
			if (arg0.getKeyChar() == '\n'){
				reloadFrame();
			} else {
				addToCode(arg0.getKeyChar());
			}
			arg0.consume();
		}
		
		@Override
		public void keyReleased(KeyEvent arg0) {
			arg0.consume();
			
		}
		
		@Override
		public void keyPressed(KeyEvent arg0) {
			arg0.consume();
			
		}
	}
}
